// 工具包
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime' // 到指定时间需要的插件
import 'dayjs/locale/zh-cn' // import

// 返回多久之前
export const timeAgo = (targetTime) => {
  // 格式化时间
  dayjs.extend(relativeTime)
  dayjs.locale('zh-cn')
  var a = dayjs()
  var b = dayjs(targetTime)
  return a.to(b)
}

// 日期格式化
export const formatDate = (e) => {
  return dayjs(e).format('YYYY-MM-DD')
}

// 时间戳封装
export const timestamp = () => (new Date()).getTime()

// 节流
export const throttle = (fn, wait) => {
  var timer = null
  return function () {
    var context = this
    var args = arguments
    if (!timer) {
      timer = setTimeout(function () {
        fn.apply(context, args)
        timer = null
      }, wait)
    }
  }
}
